package cn.javacc.common.web;

import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import org.apache.commons.codec.binary.Hex;

/**
 * 
 * @author mzh2008@163.com 2012-11-13 下午10:28:35
 * 
 */
public class EncUtils {
	private static Map<String, Cipher> enciphers = new HashMap<String, Cipher>();
	private static Map<String, Cipher> deciphers = new HashMap<String, Cipher>();
	private static String enckey = "~!@`12sklM3&*.xlK3~2*6k /.!2l " + new java.sql.Date(System.currentTimeMillis()).toString();
	public static String normalkey = "7@#`2(&$@Qqw1@";

	// 获取解密对象
	private synchronized static Cipher getDCipher(String keystr) {
		Cipher cipher = deciphers.get(keystr);
		if (cipher != null)
			return cipher;
		try {
			byte rawKey[] = keystr.getBytes();
			DESKeySpec dks = new DESKeySpec(rawKey);
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			SecretKey key = keyFactory.generateSecret(dks);
			cipher = Cipher.getInstance("DES");
			SecureRandom sr = new SecureRandom();
			cipher.init(Cipher.DECRYPT_MODE, key, sr);
			deciphers.put(keystr, cipher);
			return cipher;
		} catch (Exception e) {
			throw new RuntimeException("构建解密对象失败!", e);
		}
	}

	// 获取加密对象
	private synchronized static Cipher getECipher(String keystr) {
		Cipher ecipher = enciphers.get(keystr);
		if (ecipher != null)
			return ecipher;
		try {
			// 生成密匙
			SecureRandom sr = new SecureRandom();
			byte rawKey[] = keystr.getBytes();
			DESKeySpec dks = new DESKeySpec(rawKey);
			String algorithm = "DES";
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
			SecretKey key = keyFactory.generateSecret(dks);
			// 创建用于实际加密操作的Cipher对象
			ecipher = Cipher.getInstance(algorithm);
			ecipher.init(Cipher.ENCRYPT_MODE, key, sr);
			enciphers.put(keystr, ecipher);
			return ecipher;
		} catch (Exception e) {
			throw new RuntimeException("初始化加密对象失败!", e);
		}
	}

	/**
	 * 加密数据
	 */
	public static String encodeNormal(String content, String enckey) {
		if (content == null || content.equals(""))
			return content;
		try {
			return new String(Hex.encodeHex(getECipher(enckey).doFinal(content.getBytes())));
		} catch (Exception e) {
			throw new RuntimeException("执行加密失败!", e);
		}
	}

	/**
	 * 解密数据
	 */
	public static String decodeNormal(String content, String enckey) {
		if (content == null || content.equals(""))
			return content;
		try {
			return new String(getDCipher(enckey).doFinal(Hex.decodeHex(content.toCharArray())));
		} catch (Exception e) {
			throw new RuntimeException("执行解密失败!", e);
		}
	}

	/**
	 * 加密URL参数
	 */
	public static String encodeUrlNormal(String url, String enckey) {
		if (url == null || url.trim().equals("") || url.indexOf("?") < 0)
			return url;
		String fstr = url.substring(0, url.indexOf('?'));
		String lstr = url.substring(url.indexOf('?') + 1);
		lstr = "encstr=" + encodeNormal(lstr, enckey);
		url = fstr + "?" + lstr;
		return url;
	}

	/**
	 * 接口数据加密
	 */
	public static String encode(String content) {
		return encodeNormal(content, enckey);
	}

	/**
	 * 接口数据解密
	 */
	public static String decode(String content) {
		return decodeNormal(content, enckey);
	}

	/**
	 * Url加密
	 */
	public static String encodeUrl(String url) {
		return encodeUrlNormal(url, enckey);
	}

	public static String toChar(Long num) {
		String s = num + "";
		String rstr = "";
		for (int i = 0, len = s.length(); i < len; i++) {
			rstr = rstr + (char) ('a' + (s.charAt(i) - '0'));
		}
		return rstr;
	}

	public static Long toLong(String str) {
		String s = "";
		for (int i = 0, len = str.length(); i < len; i++) {
			s = s + (int) (str.charAt(i) - 'a');
		}
		return Long.valueOf(s);
	}
}
